const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    content: "",
    order_no: null
  },

  delete_img(e) {
    let index = e.detail.index
    console.log(e.detail.index);
    let imgList = this.data.imgList
    imgList.splice(index, 1)
    this.setData({ imgList })
  },

  afterRead(event) {
    let that = this
    console.log(event);
    const {
      file
    } = event.detail;
    wx.uploadFile({
      url: 'https://vip.join.ink/api/upload/img',
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'upload'
      },
      success(res) {
        let url = JSON.parse(res.data)
        url = "https://vip.join.ink/" + url.path
        console.log(url, "url");
        let {
          imgList
        } = that.data;
        imgList.push({
          url: url
        })
        that.setData({
          imgList
        });
        console.log(imgList);
      },
    });
  },

  submit() {

    let { imgList, content ,order_no} = this.data
    imgList=imgList.map(item=>{
      return item.url
    })
    imgList=imgList.join(',')
    console.log({
      imgList, content,order_no
    });
    app.request("/user/add_order_comment", {
      method: "post",
      data: {
        order_no,
        type: 1,
        img_one:imgList,
        contents: content
      }
    }).then(res=>{
      console.log(res);
      if(res.data.code==200){
        wx.redirectTo({
          url: '/pages/order_list/order_list',
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      order_no:options.order_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})